﻿Public Module UserDAL
    ''' <summary>
    ''' 登陆
    ''' </summary>
    ''' <param name="user"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Login(ByVal user As UserInfo) As Boolean

        Dim sql As String = String.Format("select * from UserInfo where UserName='{0}' and Password='{1}' and role='管理员'",
                                          user.UserName,
                                          user.PassWord
                                          )

        If AccessHelper.QuerySQL(sql) Then
            Return True
        Else
            Return False
        End If
        ' Return False
    End Function
    ''' <summary>
    ''' 获取所有用户信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetUserInfo() As DataSet
        Dim sql As String = String.Format("select * from UserInfo")

        Return AccessHelper.ExecuteSQL(sql)
    End Function

    Function Regitser(ByVal user As UserInfo) As Integer
        Dim sql As String = String.Format("insert into userinfo(username,[password],birthtime,role,sex,address,state)" +
                                         "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                         user.UserName,
                                         user.PassWord,
                                         user.BirthTime,
                                         user.role,
                                         user.Sex,
                                         user.Address,
                                         user.State)

        'sql = "insert into userinfo(username,[password],birthtime,role,sex,address,state) values('123','123','2013-07-19','管理员','女','23','1')"


        Return AccessHelper.Execute(sql)
    End Function

    Function GetUserInfoByID(ByVal ID As String) As DataSet
        Dim sql As String = String.Format("select * from userinfo where id={0}", ID)
        Return AccessHelper.ExecuteSQL(sql)
    End Function

    Function UpdateUser(ByVal user As UserInfo) As Boolean
        Dim sql As String = String.Format("update userinfo set username='{0}',[password]='{1}',birthtime='{2}',role='{3}',sex='{4}',address='{5}',state='{6}' where id={7}",
                                         user.UserName,
                                         user.PassWord,
                                         user.BirthTime,
                                         user.role,
                                         user.Sex,
                                         user.Address,
                                         user.State,
                                         user.ID
                                        )
        If String.IsNullOrEmpty(user.PassWord) Then
            sql = String.Format("update userinfo set username='{0}',birthtime='{1}',role='{2}',sex='{3}',address='{4}',state='{5}' where id={6}",
                                         user.UserName,
                                         user.BirthTime,
                                         user.role,
                                         user.Sex,
                                         user.Address,
                                         user.State,
                                         user.ID
                                        )
        End If
        Return AccessHelper.Execute(sql)
        Return False
    End Function

    Function DeleteUsers(ByVal idList As List(Of Integer)) As Boolean
        Dim stridList As New System.Text.StringBuilder
        stridList.Append("(")
        For Each item As Int32 In idList           
            stridList.Append(item.ToString + ",")
        Next
        stridList.Remove(stridList.Length - 1, 1)
        stridList.Append(")")
        Dim sql As String = String.Format("delete  from userinfo where id  in{0}", stridList)
        Return AccessHelper.Execute(sql)

        Return False
    End Function
End Module
